package com.stone.mythread;

import java.util.concurrent.TimeUnit;

public class DeadLock {

    public static void main(String[] args) {
        StringBuffer s1 = new StringBuffer();
        StringBuffer s2 = new StringBuffer();

        new Thread(() -> {
            synchronized (s1) {
                s1.append("a");
                s2.append("1");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (s2) {
                    s1.append("b");
                    s2.append("2");
                    System.err.println(s1);
                    System.err.println(s2);
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (s2) {
                s1.append("c");
                s2.append("3");
                synchronized (s1) {
                    s1.append("d");
                    s2.append("4");
                    System.err.println(s1);
                    System.err.println(s2);
                }
            }
        }).start();
    }
}
